package com.api.shopapi.mapper;

import com.api.shopapi.entity.DeviceYjsbjian;
import com.api.shopapi.entity.dto.DeviceQzXuetangyiDTO;
import com.api.shopapi.entity.dto.DeviceYjsbjianDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface DeviceYjsbjianMapper {

    @Insert("insert into device_yjsbjian (DeviceYjsbjianCode,UserCode,DeviceId,BindTime) values " +
            "(#{deviceYjsbjian.DeviceYjsbjianCode},#{deviceYjsbjian.UserCode},#{deviceYjsbjian.DeviceId},#{deviceYjsbjian.BindTime})")
    int insert(@Param("deviceYjsbjian") DeviceYjsbjian deviceYjsbjian);

    @Update("update device_yjsbjian set UserCode=#{deviceYjsbjian.UserCode},DeviceId=#{deviceYjsbjian.DeviceId},BindTime=#{deviceYjsbjian.BindTime} " +
            "where DeviceYjsbjianCode=#{deviceYjsbjian.DeviceYjsbjianCode}")
    int update(@Param("deviceYjsbjian") DeviceYjsbjian deviceYjsbjian);

    @Select("select * from device_yjsbjian where UserCode=#{userCode} limit 1")
    DeviceYjsbjian getByUserCode(@Param("userCode") String userCode);

    @Select("select * from device_yjsbjian where DeviceId=#{deviceId} limit 1")
    DeviceYjsbjian getByDeviceId(@Param("deviceId") String deviceId);

    @Select("<script>" +
            "select dy.*,u.Name as UserName,u.Phone from device_yjsbjian dy left join user u on dy.UserCode = u.UserCode " +
            "left join user_device ud on ud.UserCode = dy.UserCode " +
            "where dy.DeviceId is not null " +
            "and ud.YJSBJian = 1 " +
            "<if test ='phone !=null and phone!=\"\" '>" +
            " and u.Phone like concat('%',#{phone},'%') " +
            "</if>" +
            "<if test ='deviceId !=null and deviceId!=\"\" '>" +
            " and dy.DeviceId =#{deviceId} " +
            "</if>" +
            "</script>")
    List<DeviceYjsbjianDTO> getPage(@Param("phone") String phone, @Param("deviceId") String deviceId);
}
